package logo

import (
	"fmt"
	"io"
	"os"
)

type Printer interface {
	SetOutput(writer io.Writer)
	Print(content []byte) error
}

type StdPrinter struct {
	writer io.Writer
}

type ColorPrinter struct {
	StdPrinter
	color string
}

func NewColorPrinters() []Printer {
	return []Printer{
		LevelDebug: NewPrinter(io.Discard, "36m"),
		LevelInfo:  NewPrinter(os.Stdout, "32m"),
		LevelWarn:  NewPrinter(os.Stdout, "33m"),
		LevelError: NewPrinter(os.Stdout, "31m"),
	}
}

func NewStdPrinters() map[LogLevel]Printer {
	return map[LogLevel]Printer{
		LevelDebug: NewPrinter(io.Discard),
		LevelInfo:  NewPrinter(os.Stdout),
		LevelWarn:  NewPrinter(os.Stdout),
		LevelError: NewPrinter(os.Stdout),
	}
}

func NewPrinter(writer io.Writer, color ...string) Printer {
	if len(color) > 0 {
		return &ColorPrinter{
			StdPrinter: StdPrinter{writer: writer},
			color:      color[0],
		}
	}
	return &StdPrinter{writer: writer}
}

func (s *StdPrinter) SetOutput(writer io.Writer) {
	s.writer = writer
}

func (s *StdPrinter) Print(message []byte) error {
	_, err := fmt.Fprintf(s.writer, "%s", message)
	return err
}

func (s *ColorPrinter) Print(message []byte) error {
	_, err := fmt.Fprintf(s.writer, "\033[%s%s\033[0m", s.color, message)
	return err
}
